package wyq.gy;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVGeoPoint;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.FindCallback;
import com.avos.avoscloud.GetCallback;
import com.avos.avoscloud.SaveCallback;

import java.text.SimpleDateFormat;
import java.util.List;

import wyq.gy.Bean.workerInfo;

public class ReleaseOrderActivity extends AppCompatActivity {
    private String m_address;
    private double m_latitude;
    private double m_longitude;
    private TextView m_poi_tx;
    private EditText m_order_name;
    private EditText m_order_info;

    private String m_orderId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_release_order);
        android.support.v7.app.ActionBar actionBar = getSupportActionBar();
        if(actionBar != null){
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }

        Init();
        m_orderId = getIntent().getStringExtra("orderId");
        if(m_orderId != null) {
            GetOrderDetail();
        }
    }

    private void Init(){
        m_order_name = (EditText) findViewById(R.id.order_name);
        m_order_info = (EditText) findViewById(R.id.order_info);
        m_poi_tx = (TextView)findViewById(R.id.order_poi);
        m_poi_tx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(ReleaseOrderActivity.this, GetPositionActivity.class);
                startActivityForResult(intent,12);
            }
        });
        Button btn = (Button)findViewById(R.id.new_order_btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //实际注册
                AVUser user = AVUser.getCurrentUser();
                AVObject order = new AVObject("MyOrderList");
                order.put("RepairCategory",m_order_name.getText());
                order.put("ServiceContent",m_order_info.getText());
                AVGeoPoint point = new AVGeoPoint(m_latitude, m_longitude);
                order.put("Location",point);
                order.put("RealAddress",m_address);

                order.put("Status","1");
                SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                String date = sDateFormat.format(new java.util.Date());
                order.put("PublicTime",date);
                order.put("EmployerID",user.getUsername());
                order.put("EmployerName",user.get("Name"));
                order.saveInBackground(new SaveCallback() {
                    @Override
                    public void done(AVException e) {
                        if (e == null) {
                            finish();
                            Toast.makeText(ReleaseOrderActivity.this,"发布成功", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(ReleaseOrderActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });

            }
        });
    }

    private void GetOrderDetail(){
        AVQuery<AVObject> query = new AVQuery<>("MyOrderList");
        query.getInBackground(m_orderId, new GetCallback<AVObject>() {
            @Override
            public void done(AVObject avObject, AVException e) {
                int state = Integer.parseInt(avObject.getString("Status"));
                if(state == 1){
                    m_order_info.setText(avObject.getString("ServiceContent"));
                    m_order_name.setText(avObject.getString("RepairCategory"));
                    m_poi_tx.setText(avObject.getString("RealAddress"));
                    m_address = avObject.getString("RealAddress");
                    AVGeoPoint point = avObject.getAVGeoPoint("Location");
                    m_latitude = point.getLatitude();
                    m_longitude = point.getLongitude();
                }
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish(); // back button
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode==12 && resultCode==10){
            if(data != null) {
                Bundle bundle = data.getBundleExtra("addr");
                m_address = bundle.getString("address");
                m_latitude = bundle.getDouble("latitude");
                m_longitude = bundle.getDouble("longitude");
                m_poi_tx.setText(m_address);
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
